SELECT COLUMNS
library(dplyr)
# Keep only 2 columns
select(porgs, id, age)
# Drop the mass column
select(porgs, -mass)
# Put the age column first, but
# keep everything else the same
select(porgs, age, everything())
SORT ROWS
# Sort by age w/ YOUNGEST on top
arrange(porgs, age)
# Sort by age w/ ELDEST on top
arrange(porgs, desc(age))
# Sort by color and then by age
arrange(porgs, color, desc(age))

DATES
Convert text to Date
Function Order of Input Output
mdy() Month-Day-Year :: 05-18-2019 2019-05-18
mdy_hm() Month-Day-Year Hour:Minutes :: 05-18-2019 8:35 2019-05-18 08:35:00 UTC
mdy_hms() Month-Day-Year Hour:Mins:Secs :: 05-18-2019 8:35:22 2019-05-18 08:35:22 UTC
Date parts
Function Date element
year() Year
month() Month as 1,2,3
day() Day of the month
wday() Day of the week
hour() Hour of the day (24hr)
tz() Time zone
JOIN TABLES

left_join() keeps all rows and columns in the left table, but only keeps rows in the right table that match.

# Table w/ porg ages and heights
porgs

# Table w/ porg names
porg_names

# Join together by id columns
together <- left_join(porgs, 
                      porg_names, 
                      by = "id")

SAVE DATA
Data tables
library(readr)
# Save data to a CSV text file
write_csv(porgs, "my_porg_data.csv")
Plots and images
library(ggsave)
# Save the last plot you viewed
ggsave("2019_porg_plot.png")
# Save an earlier named plot
ggsave(best_plot, "the_best_plot.png")
HELP!
Online
From R
  • Go to Help > Cheatsheets
  • Type ? in the Console
# Function help
?read_csv
# Search help
help.search("boxplot")
SHORTCUTS
  • Run line: CTRL+ENTER
  • Save script: CTRL+S
  • Tidy code: highlight+CTRL+Shift+A
Find data

R COMMUNITY
  • #rstats on
  • ROpenSci.org
  • RLadies.org
  • R-Bloggers.com
  • useR Conferences


IFELSE: YES/NO CONDITIONS

Use ifelse() to create new values that depend on the value of another column. For example, to only label the porgs with a height over 60cm as “tall”.

# If a porg's height is > 60cm label it as "tall", 
# otherwise label as "short"
mutate(porgs, label = ifelse(height > 60, "tall", "short"))
THE R TEAM

Melinda.Ronca-Battista

Jaime.Yazzie

Vallen.Cook

Kristie.Ellickson

Dorian.Kvale